package com.yc;

import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import dev.langchain4j.model.input.structured.StructuredPrompt;
import dev.langchain4j.model.input.structured.StructuredPromptProcessor;
import dev.langchain4j.model.openai.OpenAiChatModel;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.time.Duration.ofSeconds;

public class _06_PromptTemplate {
    //1.简单模板
    static class Simple_Prompt_Template_Example{
        public static void main(String[] args) {
            String apiKey = System.getenv("DEEPSEEK_API_KEY");

            OpenAiChatModel model = OpenAiChatModel.builder()
                   .apiKey(apiKey)
                   .modelName("deepseek-chat")
                    .baseUrl("https://api.deepseek.com")
                    .logRequests(true)
                   .logResponses(true)
                    .timeout(ofSeconds(60))
                   .build();

            //dishType:菜的类型
            //ingredients:菜的原料
            String template="Create a recipe for a {{dishType}} dish with the following ingredients:{{ingredients}}";
            PromptTemplate promptTemplate=PromptTemplate.from(template);
            //填充变量
            Map<String,Object> variables=new java.util.HashMap<>();
            variables.put("dishType","oven dish");
            variables.put("ingredients","flour, eggs, milk, butter, salt, pepper");

            //生成提示词
            Prompt prompt = promptTemplate.apply(variables);

            String response = model.chat(prompt.text());
            System.out.println(prompt);
        }
    }

    //2.复杂模板
     static class Complex_Prompt_Template_Example {
         @StructuredPrompt({
                 "create a recipe for a {{dish}} dish with the following ingredients:{{ingredients}}",
                 "Structure your answer in the following way:",
                 "Recpie name...",
                 "Description...",
                 "Preparation time...",
                 "Required ingredients...",
                 "Instructions..."
         })
         static class CreateRecipePrompt {
             String dish;
             List<String> ingredients;

             CreateRecipePrompt(String dish, List<String> ingredients) {
                 this.dish = dish;
                 this.ingredients = ingredients;
             }
         }

         public static void main (String[]args){
             String apiKey = System.getenv("DEEPSEEK_API_KEY");

             OpenAiChatModel model = OpenAiChatModel.builder()
                     .apiKey(apiKey)
                     .modelName("deepseek-chat")
                     .baseUrl("https://api.deepseek.com")
                     .logRequests(true)
                     .logResponses(true)
                     .timeout(ofSeconds(60))
                     .build();

             CreateRecipePrompt createRecipePrompt = new CreateRecipePrompt("oven dish", List.of("flour", "eggs", "milk", "butter", "salt", "pepper"));
             Prompt prompt = StructuredPromptProcessor.toPrompt(createRecipePrompt);
             String response = model.chat(prompt.text());
             System.out.println(response);
         }
     }
     }
